//对于axios进行二次封装
import axios from 'axios'
//引入进度条插件
import nProgress from 'nprogress'
//引入进度条nProgress的样式
import "nprogress/nprogress.css"
import store from '@/store'
//创建一个axios的实例对象
const requests = axios.create({
  //配置信息
  baseURL: '/api',//基础路径
  timeout: 5000,//设置请求超时时间
})

//请求拦截器interceptors
requests.interceptors.request.use((config) => {
  if (store.state.detail.uuid_token) {
    config.headers.userTempId = store.state.detail.uuid_token
  }

  if (store.state.user.token) {
    config.headers.token = store.state.user.token
  }
  //进度条开始动
  nProgress.start()
  //config:配置对象，里面有请求头Headers
  return config
})

//响应拦截器
requests.interceptors.response.use(
  (res) => {
    //接收到返回的数据，进度条结束
    nProgress.done()
    return res.data
  },
  (error) => {
    return Promise.reject(new Error('fiale'))
  }
)

export default requests